app.controller('TerminalListCtrl', function ($scope, ajax, $modal, toaster) {

	$scope.reload = $scope.page.loader('terminal/bk/{pageIndex}/{pageSize}/page');
	$scope.$on('pageIndex', function (event, p) { $scope.reload(p, $scope.queryMap); });
	$scope.reload(1);
	$scope.queryMap = {};
	$scope.query = function () { $scope.reload(1, $scope.queryMap); };

	//同步站点
	$scope.sync = function () {
		var url = 'terminal/bk/sync';
		ajax.call(new Req.POST({ resource: url })).then(function (result) {
			if (result.code == 200) {
				toaster.pop('success', '成功', '同步信息成功。');
				$scope.reload(1);
			} else {
				toaster.pop('error', '错误', '同步信息失败！');
			}
		}, function () {
			toaster.pop('error', '错误', '同步信息失败！');
		});
	};

	$scope.onoff = function (terminal) {
		var url = 'terminal/bk/' + terminal.id + '/switch';
		ajax.call(new Req.PUT({ resource: url })).then(function (result) {
			if (result.code == 200) {
				terminal.sellable = !terminal.sellable;
				return true;
			} else {
				toaster.pop('error', '错误', result.result);
				return false;
			}
		}, function () {
			toaster.pop('error', '错误', '修改可售状态失败！');
			return false;
		});
	};
});
